package com.polonium.eclipserunner.webdriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import static com.polonium.eclipserunner.webdriver.Browser.HTML_UNIT;

/**
 * Create once static Driver object. Type dependent on static DEFAULT_BROWSER
 * field
 * 
 * @author Marek Serwanski
 */
public class DriverFactory {
	// default browser is firefox
	public static Browser[] TARGET_BROWSERS = {};
	public static Browser CURRENT_BROWSER = HTML_UNIT;
	public static Browser DEFAULT_BROWSER = HTML_UNIT;
	private static WebDriver WEB_DRIVER;

	//remember to provide servers when needed
	public static WebDriver getDriver() {
		if (WEB_DRIVER == null) {
			switch (CURRENT_BROWSER) {
			case HTML_UNIT:
				WEB_DRIVER = new HtmlUnitDriver();
				break;
			case FIREFOX:
				WEB_DRIVER = new FirefoxDriver();
				break;
			case IEXPLORER:
				WEB_DRIVER = new InternetExplorerDriver();
				break;
			case CHROME:
				WEB_DRIVER = new ChromeDriver();
				break;
			}
		}
		return WEB_DRIVER;
	}

	public static void clearDriver() {
		WEB_DRIVER = null;
	}
	
	public static void setDefaultBrowser(){
		TARGET_BROWSERS = new Browser[] {DEFAULT_BROWSER};
	}
}
